/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.DocumentPropertiesController;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import model.Base;
import model.Document;
import model.Observer;
import net.miginfocom.swing.MigLayout;

/**
 *
 * @author Ira
 */
public class DocumentPropertiesView extends JPanel implements Observer {
	
	//<editor-fold defaultstate="collapsed" desc="noms des composants">
	public static final String BTN_SAVE = "btn_save";
	public static final String BTN_BORROW = "btn_borrow";
	public static final String BTN_RESERVE = "btn_reserve";
	public static final String BTN_BRING_BACK = "btn_bring_back";
	public static final String BTN_NO_RESERVE = "btn_no_reserve";
	public static final String BTN_DELETE = "btn_delete";
	//</editor-fold>
 

	private DocumentPropertiesController controller;
	private Document doc;
	private DocumentPropertiesField content = null;
	private DocumentPropertiesBook book;
	private DocumentPropertiesCD cd;
	private DocumentPropertiesDVD dvd;
	private DocumentPropertiesMagazine magazine;
	private DocumentPropertiesNewsPaper newsPaper;
	private Base model;
	
	private JLabel lblTitle = new JLabel("Titre");
	private JLabel lblEditor = new JLabel("Editeur");
	private JLabel lblAuthors = new JLabel("Auteurs");
	private JLabel lblCategory = new JLabel("Catégorie");
	private JLabel lblYear = new JLabel("Année");
	
	private boolean editable;
	private View mainWindow;
	
	private JButton btnSave = new JButton("Enregistrer les modifs");
	private JButton btnBorrow = new JButton("Emprunter");
	private JButton btnReserve = new JButton("Réserver");
	private JButton btnBringBack = new JButton("Rendre");
	private JButton btnNoReserve = new JButton("Annuler la réservation");
	private JButton btnDelete = new JButton("Supprimer");
	
	private JTextField txtTitle = new JTextField(13);
	private JTextField txtEditor = new JTextField(13);
	private JTextField txtAuthors = new JTextField(13);
	private JTextField txtCategory = new JTextField(13);
	private JTextField txtYear = new JTextField(13);
	
	public DocumentPropertiesView(Base model, boolean editable, View mainWindow) {
		
		this.mainWindow = mainWindow;
		this.controller = new DocumentPropertiesController(model, this);
		this.model = model;
		this.editable = editable;
		this.book = new DocumentPropertiesBook(editable);
		this.cd = new DocumentPropertiesCD(editable);
		this.dvd = new DocumentPropertiesDVD(editable);
		this.magazine = new DocumentPropertiesMagazine(editable);
		this.newsPaper = new DocumentPropertiesNewsPaper(editable);
		
		// Nom des composants
		this.btnBorrow.setName(DocumentPropertiesView.BTN_BORROW);
		this.btnReserve.setName(DocumentPropertiesView.BTN_RESERVE);
		this.btnSave.setName(DocumentPropertiesView.BTN_SAVE);
		this.btnBringBack.setName(DocumentPropertiesView.BTN_BRING_BACK);
		this.btnNoReserve.setName(DocumentPropertiesView.BTN_NO_RESERVE);
		this.btnDelete.setName(DocumentPropertiesView.BTN_DELETE);
		
		this.txtAuthors.setEditable(editable);
		this.txtCategory.setEditable(false);
		this.txtEditor.setEditable(editable);
		this.txtTitle.setEditable(editable);
		this.txtYear.setEditable(editable);
		
		TitledBorder border = BorderFactory.createTitledBorder("Informations sur le document sélectionné");
		
		this.setLayout(new MigLayout());
		this.setBorder(border);
		this.setMinimumSize(new Dimension(440, 165));
		this.setPreferredSize(new Dimension(440, 165));
		this.setMaximumSize(new Dimension(440, 165));
		
		this.add(this.lblTitle, "w 60!");
		this.add(this.txtTitle);
		this.add(this.lblCategory, "w 60!");
		this.add(this.txtCategory, "wrap");
		this.add(this.lblAuthors, "w 60!");
		this.add(this.txtAuthors);
		this.add(this.lblEditor, "w 60!");
		this.add(this.txtEditor, "wrap");
		this.add(this.lblYear, "w 60!");
		this.add(this.txtYear, "wrap");
		
		// Listener
		this.btnBorrow.addActionListener(this.controller);
		this.btnReserve.addActionListener(this.controller);
		this.btnSave.addActionListener(this.controller);
		this.btnBringBack.addActionListener(this.controller);
		this.btnNoReserve.addActionListener(this.controller);
		this.btnDelete.addActionListener(this.controller);
	}
	
	public void setContent(Document d) {
		this.doc = d;
		String authors = "";
		for(String author : d.getAuthors()) {
			authors += author + ", ";
		}
		
		this.txtAuthors.setText(authors);
		this.txtCategory.setText(d.getCategory());
		this.txtYear.setText("" + d.getYear());
		this.txtEditor.setText(d.getEditor());
		this.txtTitle.setText(d.getTitle());
		
		if(this.content != null) {
			this.remove(this.content);
		}
		
		if(d.getCategory().equals("Livre")) {
			this.content = this.book;
		}
		else if(d.getCategory().equals("Journal")) {
			this.content = this.newsPaper;
		}
		else if(d.getCategory().equals("Magazine")) {
			this.content = this.magazine;
		}
		else if(d.getCategory().equals("CD")) {
			this.content = this.cd;
		}
		else if(d.getCategory().equals("DVD")) {
			this.content = this.dvd;
		}
		
		this.content.setContent(d);
		this.add(this.content, "cell 0 3 4 1");
		this.validate();
	}
	
	@Override
	public void update(Object items) {
		if(items == null) {
			return;
		}
		this.setContent((Document)items);
		this.remove(this.btnReserve);
		this.remove(this.btnBorrow);
		this.remove(this.btnBringBack);
		this.remove(this.btnNoReserve);
		this.remove(this.btnSave);
		this.remove(this.btnDelete);
		
		if(this.editable && this.model.isDocumentModifiable(this.doc)) {
			this.btnSave.setVisible(true);
			this.btnDelete.setVisible(true);
			this.add(this.btnSave, "cell 0 4 4 1");
			this.add(this.btnDelete, "cell 0 4 4 1");			
		}
		else if(this.model.isBorrowable(this.doc)) {
			this.btnBorrow.setVisible(true);
			this.add(this.btnBorrow, "cell 0 4 4 1");
		}
		else if(this.model.isReservable(this.doc)) {
			this.btnReserve.setVisible(true);
			this.add(this.btnReserve, "cell 0 4 4 1");
		}
		else if(this.model.isBorrowed(this.doc)) {
			this.btnBringBack.setVisible(true);
			this.add(this.btnBringBack, "cell 0 4 4 1");
		}
		else if(this.model.isReserved(this.doc)) {
			this.btnNoReserve.setVisible(true);
			this.add(this.btnNoReserve, "cell 0 4 4 1");
		}
		this.mainWindow.setDocPropertiesView();
	}

	/**
	 * @return the doc
	 */
	public Document getDoc() {
		return doc;
	}

	/**
	 * @return the book
	 */
	public DocumentPropertiesBook getBook() {
		return book;
	}

	/**
	 * @return the cd
	 */
	public DocumentPropertiesCD getCd() {
		return cd;
	}

	/**
	 * @return the dvd
	 */
	public DocumentPropertiesDVD getDvd() {
		return dvd;
	}

	/**
	 * @return the magazine
	 */
	public DocumentPropertiesMagazine getMagazine() {
		return magazine;
	}

	/**
	 * @return the newsPaper
	 */
	public DocumentPropertiesNewsPaper getNewsPaper() {
		return newsPaper;
	}

	/**
	 * @return the txtTitle
	 */
	public String getTxtTitle() {
		return txtTitle.getText();
	}

	/**
	 * @return the txtEditor
	 */
	public String getTxtEditor() {
		return txtEditor.getText();
	}

	/**
	 * @return the txtAuthors
	 */
	public String getTxtAuthors() {
		return txtAuthors.getText();
	}

	/**
	 * @return the txtCategory
	 */
	public String getTxtCategory() {
		return txtCategory.getText();
	}

	/**
	 * @return the txtYear
	 */
	public String getTxtYear() {
		return txtYear.getText();
	}
}
